package santiago.view.game;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import santiago.client.Client;
import santiago.domain.ColorType;
import santiago.domain.Game;
import santiago.domain.Plantation;
import santiago.domain.Player;
import santiago.view.ResourceManager;
import santiago.view.SantiagoFrame;
import santiago.view.SantiagoValidation;

public class FrameGame extends SantiagoFrame {

    // Information components
    private JLabel[] PlayerLabels;
    private JLabel[] PlayerActionLabels;
    private int LastTextPaneChatHeight;
    // GameboardComponents
    private JLabel[] PlantationLabels;

    /**
     * Creates new form FrameGame
     */
    public FrameGame() {
        super();
        initComponents();
        setLocationRelativeTo(null);

        Game Game = Client.getGame();
        initGameboard(Game);
        initPanelInformationRound0(Game);

        TextPaneChat.setText("<span></span>");
        LastTextPaneChatHeight = TextPaneChat.getHeight();
    }

    @Override
    public void refresh() {
        Game Game = Client.getGame();
        initPlayerSequence(Game);
        byte CurrentStage = Game.getCurrentStage();
        switch (CurrentStage) {
            case 1: // auction
                auction(Game);
                break;
            case 2: // change canal overseer && plantation selection
                changeCanalOverseer(Game);
                plantationSelection(Game);
                break;
        }
    }

    private void initGameboard(Game Game) {
        // init information components
        initInformationComponents(Game);

        // init plantation labels array
        initPlantationLabels();

        // init gameboad components
        initGameboardComponents(Game);
        /*int x = LabelGameboard.getX() + 12;
         int y = LabelGameboard.getY() + 12;
         for (int j = 0; j < 3; j++) {
         for (int i1 = 0; i1 < 4; i1++) {
         listeCase.add(new FieldLabel(x, y));
         x += 92 + 4;
         listeCase.add(new FieldLabel(x, y));
         x += 92 + 12;
         }
         x = LabelGameboard.getX() + 12;
         y += 92 + 4;
         for (int i2 = 0; i2 < 4; i2++) {
         listeCase.add(new FieldLabel(x, y));
         x += 92 + 4;
         listeCase.add(new FieldLabel(x, y));
         x += 92 + 12;
         }
         y += 92 + 12;
         x = LabelGameboard.getX() + 12;
         }*/
    }

    private void initInformationComponents(Game Game) {
        // init player labels
        PlayerLabels = new JLabel[5];
        PlayerLabels[0] = LabelPlayer0;
        PlayerLabels[1] = LabelPlayer1;
        PlayerLabels[2] = LabelPlayer2;
        PlayerLabels[3] = LabelPlayer3;
        PlayerLabels[4] = LabelPlayer4;
        PlayerActionLabels = new JLabel[5];
        PlayerActionLabels[0] = LabelPlayer0Action;
        PlayerActionLabels[1] = LabelPlayer1Action;
        PlayerActionLabels[2] = LabelPlayer2Action;
        PlayerActionLabels[3] = LabelPlayer3Action;
        PlayerActionLabels[4] = LabelPlayer4Action;
        initPlayerSequence(Game);

        // disable action inputs
        SpinnerActionValue.setEnabled(false);
        ButtonValidateAction.setEnabled(false);

        // init other information
        LabelCurrentRound.setText(Game.getCurrentRound() + "");
        LabelTotalRounds.setText(Game.getTotalRounds() + "");
        LabelColorWithIcon.setIcon(ResourceManager.getYieldMarkerIcon(Client.getPlayer().getColor()));
        LabelAction.setVisible(false);
        SpinnerActionValue.setVisible(false);
        ButtonValidateAction.setVisible(false);
    }

    private void initPlantationLabels() {
        PlantationLabels = new JLabel[5];
        int X = 860;
        int Y = 70;
        for (int i = 0; i < PlantationLabels.length; i++) {
            PlantationLabels[i] = new JLabel();
            PanelDragDrop.add(PlantationLabels[i], new org.netbeans.lib.awtextra.AbsoluteConstraints(X, Y, -1, -1));
            Y += 110;
        }
    }

    private void initGameboardComponents(Game Game) {
        PanelGameboard.add(new PanelGameboard(Game), new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));
        /*// init gameboard panel
         * Graphics G = PanelGameboard.getGraphics();
         * G.drawImage(Manager.getImage(ResourceManager.Image.Gameboard), 0, 0, null);
         * PanelGameboard.repaint();
         * // init nodes
         * List<Node> Nodes = Game.getNodes();
         * boolean SpringFounded = false;
         * int X = 0;
         * int Y = 0;
         * for (int i = 0; i < Nodes.size() & !SpringFounded; i++) {
         * if ((SpringFounded = Nodes.get(i).isSpring())) {
         * }
         * if (i > 0 && (i + 1) % 5 == 0) {
         * X = 0;
         * Y += 200;
         * } else {
         * X += 200;
         * }
         * }*/
    }

    private void initPanelInformationRound0(Game Game) {
        if (Game.getDeletedPlantation() != null) {
            LabelDeletedPlantation.setText(Game.getDeletedPlantation() + " a été supprimée !");
            LabelDeletedPlantation.setForeground(Color.RED);
        }
        TablePlayers = new JTable() {
            private static final long serialVersionUID = 1L;

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        TablePlayers.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable Table, Object Value, boolean IsSelected, boolean HasFocus, int Row, int Column) {
                Component Cell = super.getTableCellRendererComponent(Table, Value, IsSelected, HasFocus, Row, Column);
                Color C;
                if ((C = ColorType.getColor(Value.toString())) != null) {
                    Cell.setForeground(C);
                    Cell.setBackground(C);
                } else {
                    Cell.setForeground(Color.BLACK);
                    Cell.setBackground(Color.WHITE);
                }
                return Cell;
            }
        });
        TablePlayers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        String[][] Data = null;
        String Col[] = {"Joueur", "Couleur"};
        DefaultTableModel Model = new DefaultTableModel(Data, Col);
        Collection<Player> CurrentPlayers = Game.getPlayers();
        int MaximumNumberOfPlayers = Game.getParameters().getPlayerCount();
        int i = 0;
        for (Player Player : CurrentPlayers) {
            Model.addRow(new String[]{Player.getName(), Player.getColor().toString()});
            i++;
        }
        while (i != MaximumNumberOfPlayers) {
            Model.addRow(new String[]{"En attente...", "En attente.."});
            i++;
        }
        TablePlayers.setModel(Model);
        ScrollPaneTableContainer.getViewport().removeAll();
        ScrollPaneTableContainer.getViewport().add(TablePlayers);
    }

    private void initPlayerSequence(Game Game) {
        Player[] Players = Game.getPlayerSequence();
        for (int i = 0; i < Players.length; i++) {
            Player Player = Players[i];
            PlayerLabels[i].setForeground(Player.getColor().getColor());
            PlayerActionLabels[i].setText("");
            if (i == Game.getCurrentPlayerIndex()) {
                PlayerLabels[i].setText("*" + Player.getName());
                PlayerLabels[i].setFont(new Font(PlayerLabels[i].getFont().getFontName(), Font.BOLD, PlayerLabels[i].getFont().getSize()));
            } else {
                PlayerLabels[i].setText(Player.getName());
            }
        }
        for (int i = Players.length; i < PlayerLabels.length; i++) {
            PlayerLabels[i].setVisible(false);
            PlayerActionLabels[i].setVisible(false);
        }
    }

    private void auction(Game Game) {
        LabelStageName.setText("Enchères des plantations");
        LabelAction.setText("Votre enchère :");
        Plantation[] PlantationLot = Game.getCurrentPlantationLot();
        for (int i = 0; i < PlantationLot.length; i++) {
            Plantation P = PlantationLot[i];
            PlantationLabels[i].setIcon(ResourceManager.getPlantationIcon(P.getType(), P.getCapacity(), P.getYieldMarkersCount(), null));
        }
        Player[] Players = Game.getPlayerSequence();
        for (int i = 0; i < Players.length; i++) {
            int Bid = Game.getPlayerBid(Players[i].getName());
            if (Bid != -1) {
                if (Bid == 0) {
                    PlayerActionLabels[i].setText("passe");
                } else {
                    PlayerActionLabels[i].setText("a enchéri pour " + Bid);
                }
            }
        }
        if (Client.getLogin().equals(Game.getCurrentPlayer().getName())) {
            SpinnerActionValue.setEnabled(true);
            ButtonValidateAction.setEnabled(true);
        } else {
            SpinnerActionValue.setEnabled(false);
            ButtonValidateAction.setEnabled(false);
        }
    }

    private void changeCanalOverseer(Game Game) {
        Player CanalOverseer = Game.getCanalOverseer();
        if (Client.getLogin().equals(CanalOverseer.getName())) {
            LabelCanalOverseerName.setText("Vous !");
            LabelCanalOverseerName.setForeground(new Color(0, 204, 0));
            LabelCanalOverseerWithIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/santiago/ressources/negrito-actif.png")));
        } else {
            LabelCanalOverseerName.setText(CanalOverseer.getName());
            LabelCanalOverseerName.setForeground(CanalOverseer.getColor().getColor());
            LabelCanalOverseerWithIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/santiago/ressources/negrito-inactif.png")));
        }
        SpinnerActionValue.setEnabled(false);
        ButtonValidateAction.setEnabled(false);
    }

    private void plantationSelection(Game Game) {
        LabelStageName.setText("Placement des plantations");
        LabelAction.setText("Placement :");
        SpinnerActionValue.setVisible(false);
        if (Client.getLogin().equals(Game.getCurrentPlayer().getName())) {
            // TO DO : autoriser placement plantations
            SpinnerActionValue.setEnabled(true);
            ButtonValidateAction.setEnabled(true);
        } else {
            SpinnerActionValue.setEnabled(false);
            ButtonValidateAction.setEnabled(false);
        }
    }

    private void bribeCanalOverseer(Game Game) {
        LabelStageName.setText("Soudoiement du constructeur de canal");
        LabelAction.setText("Votre offre :");
        if (Client.getLogin().equals(Game.getCurrentPlayer().getName())) {
            // TO DO : autoriser placement canal et proposition montant
            SpinnerActionValue.setEnabled(true);
            ButtonValidateAction.setEnabled(true);
        } else {
            SpinnerActionValue.setEnabled(false);
            ButtonValidateAction.setEnabled(false);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TextFieldMessage = new javax.swing.JTextField();
        ScrollPaneChat = new javax.swing.JScrollPane();
        TextPaneChat = new javax.swing.JTextPane();
        LabelChatTitle = new javax.swing.JLabel();
        ButtonValidateAction = new javax.swing.JButton();
        LabelCanalOverseerWithIcon = new javax.swing.JLabel();
        LabelSupplementalCanalWithIcon = new javax.swing.JLabel();
        LabelSupplementalCanal = new javax.swing.JLabel();
        LabelCanalOverseer = new javax.swing.JLabel();
        LabelIsSupplementalCanal = new javax.swing.JLabel();
        LabelCanalOverseerName = new javax.swing.JLabel();
        LabelStageName = new javax.swing.JLabel();
        LabelEscudos = new javax.swing.JLabel();
        labelBillets = new javax.swing.JLabel();
        LabelEscudosAmount = new javax.swing.JLabel();
        LabelColor = new javax.swing.JLabel();
        LabelColorWithIcon = new javax.swing.JLabel();
        PanelInformation = new javax.swing.JPanel();
        ScrollPaneTableContainer = new javax.swing.JScrollPane();
        TablePlayers = new javax.swing.JTable();
        LabelGameStarting = new javax.swing.JLabel();
        LabelDeletedPlantation = new javax.swing.JLabel();
        LabelStartingPlayer = new javax.swing.JLabel();
        PanelDragDrop = new javax.swing.JPanel();
        PanelGameboard = new javax.swing.JPanel();
        LabelTotalRounds = new javax.swing.JLabel();
        LabelRoundSeparator = new javax.swing.JLabel();
        LabelAvailablePlantations = new javax.swing.JLabel();
        LabelRoundCount = new javax.swing.JLabel();
        LabelCurrentRound = new javax.swing.JLabel();
        LabelAction = new javax.swing.JLabel();
        ButtonSendMessage = new javax.swing.JButton();
        PanelPlayerActions = new javax.swing.JPanel();
        LabelPlayer4 = new javax.swing.JLabel();
        LabelPlayer0 = new javax.swing.JLabel();
        LabelPlayer1 = new javax.swing.JLabel();
        LabelPlayer2 = new javax.swing.JLabel();
        LabelPlayer3 = new javax.swing.JLabel();
        LabelPlayer0Action = new javax.swing.JLabel();
        LabelPlayer1Action = new javax.swing.JLabel();
        LabelPlayer4Action = new javax.swing.JLabel();
        LabelPlayer2Action = new javax.swing.JLabel();
        LabelPlayer3Action = new javax.swing.JLabel();
        SpinnerActionValue = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Santiago");
        setBackground(new java.awt.Color(153, 102, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(java.awt.Color.white);
        setIconImages(null);
        setMinimumSize(new java.awt.Dimension(1280, 760));
        setName("FenetreDuJeu"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        TextFieldMessage.setFont(new java.awt.Font("Lucida Sans", 0, 12)); // NOI18N
        TextFieldMessage.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TextFieldMessageKeyPressed(evt);
            }
        });
        getContentPane().add(TextFieldMessage, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 520, 240, 30));

        ScrollPaneChat.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        ScrollPaneChat.setToolTipText("");
        ScrollPaneChat.setDoubleBuffered(true);
        ScrollPaneChat.setHorizontalScrollBar(null);
        ScrollPaneChat.setName("TextPaneChat"); // NOI18N

        TextPaneChat.setEditable(false);
        TextPaneChat.setContentType("text/html"); // NOI18N
        TextPaneChat.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        TextPaneChat.setText("<html>\r<head>\r</head>\r<body>\r</body></html>\r ");
        TextPaneChat.setDoubleBuffered(true);
        TextPaneChat.setFocusable(false);
        TextPaneChat.setRequestFocusEnabled(false);
        ScrollPaneChat.setViewportView(TextPaneChat);

        getContentPane().add(ScrollPaneChat, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 380, 240, 140));

        LabelChatTitle.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelChatTitle.setText("Canal de discussion");
        getContentPane().add(LabelChatTitle, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 340, -1, -1));

        ButtonValidateAction.setFont(new java.awt.Font("Lucida Sans", 0, 10)); // NOI18N
        ButtonValidateAction.setText("Valider");
        ButtonValidateAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonValidateActionActionPerformed(evt);
            }
        });
        getContentPane().add(ButtonValidateAction, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 160, -1, 30));

        LabelCanalOverseerWithIcon.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelCanalOverseerWithIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/santiago/ressources/negrito-inactif.png"))); // NOI18N
        LabelCanalOverseerWithIcon.setToolTipText("Negrito");
        getContentPane().add(LabelCanalOverseerWithIcon, new org.netbeans.lib.awtextra.AbsoluteConstraints(920, 630, -1, -1));

        LabelSupplementalCanalWithIcon.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabelSupplementalCanalWithIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/santiago/ressources/canal-bleu.png"))); // NOI18N
        getContentPane().add(LabelSupplementalCanalWithIcon, new org.netbeans.lib.awtextra.AbsoluteConstraints(522, 660, 240, 10));

        LabelSupplementalCanal.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelSupplementalCanal.setText("Canal supplémentaire :");
        getContentPane().add(LabelSupplementalCanal, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 630, -1, -1));

        LabelCanalOverseer.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelCanalOverseer.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabelCanalOverseer.setText("Constructeur :");
        getContentPane().add(LabelCanalOverseer, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 630, 140, -1));

        LabelIsSupplementalCanal.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelIsSupplementalCanal.setForeground(new java.awt.Color(0, 204, 0));
        LabelIsSupplementalCanal.setText("Disponible");
        getContentPane().add(LabelIsSupplementalCanal, new org.netbeans.lib.awtextra.AbsoluteConstraints(690, 630, -1, -1));

        LabelCanalOverseerName.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelCanalOverseerName.setForeground(new java.awt.Color(255, 0, 0));
        LabelCanalOverseerName.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabelCanalOverseerName.setText("Inconnu");
        getContentPane().add(LabelCanalOverseerName, new org.netbeans.lib.awtextra.AbsoluteConstraints(790, 650, 140, 20));

        LabelStageName.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelStageName.setText("Mise en place");
        getContentPane().add(LabelStageName, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        LabelEscudos.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelEscudos.setText("Escudos :");
        getContentPane().add(LabelEscudos, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 630, -1, -1));

        labelBillets.setIcon(new javax.swing.ImageIcon(getClass().getResource("/santiago/ressources/planche-billet.png"))); // NOI18N
        getContentPane().add(labelBillets, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 650, -1, 30));

        LabelEscudosAmount.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelEscudosAmount.setForeground(new java.awt.Color(152, 76, 74));
        LabelEscudosAmount.setText("10");
        getContentPane().add(LabelEscudosAmount, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 630, 160, -1));

        LabelColor.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelColor.setText("Couleur :");
        getContentPane().add(LabelColor, new org.netbeans.lib.awtextra.AbsoluteConstraints(980, 630, -1, -1));
        getContentPane().add(LabelColorWithIcon, new org.netbeans.lib.awtextra.AbsoluteConstraints(1050, 630, -1, -1));

        PanelInformation.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        PanelInformation.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        TablePlayers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nom du joueur", "Couleur"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        ScrollPaneTableContainer.setViewportView(TablePlayers);

        PanelInformation.add(ScrollPaneTableContainer, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 310, 108));

        LabelGameStarting.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelGameStarting.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabelGameStarting.setText("Préparez-vous la partie va commencer !");
        PanelInformation.add(LabelGameStarting, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 10, 330, -1));

        LabelDeletedPlantation.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelDeletedPlantation.setForeground(new java.awt.Color(0, 204, 0));
        LabelDeletedPlantation.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabelDeletedPlantation.setText("Pas de tuile supprimée !");
        PanelInformation.add(LabelDeletedPlantation, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 160, 330, -1));

        LabelStartingPlayer.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelStartingPlayer.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabelStartingPlayer.setText(Client.getGame().getCurrentPlayer().getName() + " commence les enchères !");
        PanelInformation.add(LabelStartingPlayer, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 190, 330, -1));

        getContentPane().add(PanelInformation, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 130, 330, 220));

        PanelDragDrop.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        PanelGameboard.setPreferredSize(new java.awt.Dimension(812, 612));
        PanelGameboard.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        PanelDragDrop.add(PanelGameboard, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        LabelTotalRounds.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelTotalRounds.setText("0");
        PanelDragDrop.add(LabelTotalRounds, new org.netbeans.lib.awtextra.AbsoluteConstraints(960, 0, -1, -1));

        LabelRoundSeparator.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelRoundSeparator.setText("/");
        PanelDragDrop.add(LabelRoundSeparator, new org.netbeans.lib.awtextra.AbsoluteConstraints(950, 0, -1, -1));

        LabelAvailablePlantations.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelAvailablePlantations.setText("Plantations aux enchères");
        PanelDragDrop.add(LabelAvailablePlantations, new org.netbeans.lib.awtextra.AbsoluteConstraints(820, 40, -1, -1));

        LabelRoundCount.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelRoundCount.setText("Tour actuel :");
        PanelDragDrop.add(LabelRoundCount, new org.netbeans.lib.awtextra.AbsoluteConstraints(840, 0, -1, -1));

        LabelCurrentRound.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelCurrentRound.setForeground(new java.awt.Color(152, 76, 74));
        LabelCurrentRound.setText("0");
        PanelDragDrop.add(LabelCurrentRound, new org.netbeans.lib.awtextra.AbsoluteConstraints(940, 0, -1, -1));

        getContentPane().add(PanelDragDrop, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 10, 1010, 612));

        LabelAction.setFont(new java.awt.Font("Lucida Sans", 1, 14)); // NOI18N
        LabelAction.setText("ActionTitle :");
        getContentPane().add(LabelAction, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 160, -1, 30));

        ButtonSendMessage.setFont(new java.awt.Font("Lucida Sans", 0, 10)); // NOI18N
        ButtonSendMessage.setText("Envoyer");
        ButtonSendMessage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonSendMessageActionPerformed(evt);
            }
        });
        getContentPane().add(ButtonSendMessage, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 550, -1, -1));

        PanelPlayerActions.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        LabelPlayer4.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        LabelPlayer4.setText("Player4");
        PanelPlayerActions.add(LabelPlayer4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, 80, -1));

        LabelPlayer0.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        LabelPlayer0.setText("Player0");
        PanelPlayerActions.add(LabelPlayer0, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 80, -1));

        LabelPlayer1.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        LabelPlayer1.setText("Player1");
        PanelPlayerActions.add(LabelPlayer1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 80, -1));

        LabelPlayer2.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        LabelPlayer2.setText("Player2");
        PanelPlayerActions.add(LabelPlayer2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, 80, -1));

        LabelPlayer3.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        LabelPlayer3.setText("Player3");
        PanelPlayerActions.add(LabelPlayer3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, 80, -1));

        LabelPlayer0Action.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        LabelPlayer0Action.setText("player 0 action");
        PanelPlayerActions.add(LabelPlayer0Action, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 10, 150, -1));

        LabelPlayer1Action.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        LabelPlayer1Action.setText("player 1 action");
        PanelPlayerActions.add(LabelPlayer1Action, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 30, 150, -1));

        LabelPlayer4Action.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        LabelPlayer4Action.setText("player 4 action");
        PanelPlayerActions.add(LabelPlayer4Action, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 90, 150, -1));

        LabelPlayer2Action.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        LabelPlayer2Action.setText("player 2 action");
        PanelPlayerActions.add(LabelPlayer2Action, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 50, 150, -1));

        LabelPlayer3Action.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        LabelPlayer3Action.setText("player 3 action");
        PanelPlayerActions.add(LabelPlayer3Action, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 70, 150, -1));

        getContentPane().add(PanelPlayerActions, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 250, 110));

        SpinnerActionValue.setModel(new javax.swing.SpinnerNumberModel(0, 0, 99, 1));
        getContentPane().add(SpinnerActionValue, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 160, 50, 30));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            Logger.getLogger(FrameGame.class.getName()).log(Level.SEVERE, null, ex);
        }
        PanelInformation.setVisible(false);
        LabelAction.setVisible(true);
        SpinnerActionValue.setVisible(true);
        ButtonValidateAction.setVisible(true);
        auction(Client.getGame());
    }//GEN-LAST:event_formWindowOpened

    private void ButtonValidateActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonValidateActionActionPerformed
        Game Game = Client.getGame();
        initPlayerSequence(Game);
        byte CurrentStage = Game.getCurrentStage();
        switch (CurrentStage) {
            case 1: // bid
                int BidAmount = (int) SpinnerActionValue.getValue();
                Client.bid(BidAmount);
                break;
            case 2: // plantation selection
                // TO DO Drag & Drop, check if plantation has been put into the gameboard etc.
                break;
        }
    }//GEN-LAST:event_ButtonValidateActionActionPerformed

    private void ButtonSendMessageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonSendMessageActionPerformed
        if (SantiagoValidation.checkEmptyField(TextFieldMessage.getText())) {
            Client.sendMessage(TextFieldMessage.getText());
        }
        TextFieldMessage.setText("");
    }//GEN-LAST:event_ButtonSendMessageActionPerformed

    private void TextFieldMessageKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TextFieldMessageKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            ButtonSendMessage.doClick();
        }
    }//GEN-LAST:event_TextFieldMessageKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonSendMessage;
    private javax.swing.JButton ButtonValidateAction;
    private javax.swing.JLabel LabelAction;
    private javax.swing.JLabel LabelAvailablePlantations;
    private javax.swing.JLabel LabelCanalOverseer;
    private javax.swing.JLabel LabelCanalOverseerName;
    private javax.swing.JLabel LabelCanalOverseerWithIcon;
    private javax.swing.JLabel LabelChatTitle;
    private javax.swing.JLabel LabelColor;
    private javax.swing.JLabel LabelColorWithIcon;
    private javax.swing.JLabel LabelCurrentRound;
    private javax.swing.JLabel LabelDeletedPlantation;
    private javax.swing.JLabel LabelEscudos;
    private javax.swing.JLabel LabelEscudosAmount;
    private javax.swing.JLabel LabelGameStarting;
    private javax.swing.JLabel LabelIsSupplementalCanal;
    private javax.swing.JLabel LabelPlayer0;
    private javax.swing.JLabel LabelPlayer0Action;
    private javax.swing.JLabel LabelPlayer1;
    private javax.swing.JLabel LabelPlayer1Action;
    private javax.swing.JLabel LabelPlayer2;
    private javax.swing.JLabel LabelPlayer2Action;
    private javax.swing.JLabel LabelPlayer3;
    private javax.swing.JLabel LabelPlayer3Action;
    private javax.swing.JLabel LabelPlayer4;
    private javax.swing.JLabel LabelPlayer4Action;
    private javax.swing.JLabel LabelRoundCount;
    private javax.swing.JLabel LabelRoundSeparator;
    private javax.swing.JLabel LabelStageName;
    private javax.swing.JLabel LabelStartingPlayer;
    private javax.swing.JLabel LabelSupplementalCanal;
    private javax.swing.JLabel LabelSupplementalCanalWithIcon;
    private javax.swing.JLabel LabelTotalRounds;
    private javax.swing.JPanel PanelDragDrop;
    private javax.swing.JPanel PanelGameboard;
    private javax.swing.JPanel PanelInformation;
    private javax.swing.JPanel PanelPlayerActions;
    private javax.swing.JScrollPane ScrollPaneChat;
    private javax.swing.JScrollPane ScrollPaneTableContainer;
    private javax.swing.JSpinner SpinnerActionValue;
    private javax.swing.JTable TablePlayers;
    private javax.swing.JTextField TextFieldMessage;
    private javax.swing.JTextPane TextPaneChat;
    private javax.swing.JLabel labelBillets;
    // End of variables declaration//GEN-END:variables

    public void displayMessage(final String Message) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    TextPaneChat.setText(TextPaneChat.getText().substring(TextPaneChat.getText().indexOf("<body>") + 6, TextPaneChat.getText().indexOf("</body>")).replaceFirst("<p>.*</p>", "") + Message);
                    TextPaneChat.scrollRectToVisible(new Rectangle(0, LastTextPaneChatHeight, 1, 1));
                    while (TextPaneChat.getHeight() < LastTextPaneChatHeight
                            || (TextPaneChat.getHeight() > LastTextPaneChatHeight && TextPaneChat.getHeight() < LastTextPaneChatHeight + TextPaneChat.getFont().getSize())) {
                        Thread.yield();
                    }
                    LastTextPaneChatHeight = TextPaneChat.getHeight();
                    TextPaneChat.scrollRectToVisible(new Rectangle(0, LastTextPaneChatHeight, 1, 1));
                    Thread.sleep(25);
                    TextPaneChat.scrollRectToVisible(new Rectangle(0, LastTextPaneChatHeight, 1, 1));
                    Thread.sleep(25);
                    TextPaneChat.scrollRectToVisible(new Rectangle(0, LastTextPaneChatHeight, 1, 1));
                } catch (InterruptedException ex) {
                }
            }
        }).start();
    }
}
